package com.telecomyt.jwportal.dialog;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ruffian.library.widget.RRelativeLayout;
import com.telecomyt.jwportal.R;

import java.util.ArrayList;
import java.util.List;

/**
 * @author created by chengDa
 * @time 2020/9/10  11:40
 * @description 键盘dialog
 */
public class KeyBoardDialog extends Dialog implements View.OnClickListener {
    private List<String> datas;
    private KeyboardAdapter adapter;
    private ImageView mImageBack;
    private RecyclerView recyclerView;
    private EditText mEditText;
    private onCallClickListener listener;

    public KeyBoardDialog(Context context, onCallClickListener listener) {
        super(context, R.style.DialogBottomStyle);
        setCanceledOnTouchOutside(false);
        setCustomDialog();
        this.listener = listener;
    }

    private void setCustomDialog() {
        View mView = LayoutInflater.from(getContext()).inflate(R.layout.dialog_key_borad, null);
        mImageBack = mView.findViewById(R.id.mImageBack);
        mEditText = mView.findViewById(R.id.mEditText);
        recyclerView = mView.findViewById(R.id.recycler_view);
        ImageView mImageDelete = mView.findViewById(R.id.mImageDelete);
        RRelativeLayout mRlCall = mView.findViewById(R.id.mRlCall);
        mImageBack.setOnClickListener(this);
        mRlCall.setOnClickListener(this);
        mImageDelete.setOnClickListener(this);
        mImageDelete.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                mEditText.setText("");
                return true;
            }
        });
        initRecycleView();
        initData();
        super.setContentView(mView);

    }

    /**
     * @date: 2020/9/10
     * @author: chengDa
     * @description: 初始化recycleView
     */
    private void initRecycleView() {
        recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 3));
        adapter = new KeyboardAdapter(R.layout.layout_keyboard_items, datas);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                mEditText.setText(String.format("%s%s", mEditText.getText().toString().trim(), datas.get(position)));
                mEditText.setSelection(mEditText.getText().length());

            }
        });
    }

    /**
     * @date: 2020/9/10
     * @author: chengDa
     * @description: 填充数据
     */
    private void initData() {
        datas = new ArrayList<>();
        for (int i = 0; i < 12; i++) {
            if (i < 9) {
                datas.add(String.valueOf(i + 1));
            } else if (i == 9) {
                datas.add("*");
            } else if (i == 10) {
                datas.add("0");
            } else {
                datas.add("#");
            }
        }
        adapter.setNewData(datas);
    }


    @Override
    public void show() {
        super.show();
        /**
         * 设置宽度全屏，要设置在show的后面
         */
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.gravity = Gravity.BOTTOM;
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        getWindow().getDecorView().setPadding(0, 0, 0, 0);
        getWindow().setAttributes(layoutParams);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.mImageBack:
                dismiss();
                break;
            case R.id.mRlCall:
                String phone = mEditText.getText().toString().trim();
                if (TextUtils.isEmpty(phone)) return;
                listener.onCall(phone);
                break;
            case R.id.mImageDelete:
                String num = mEditText.getText().toString().trim();
                if (num.length() > 0) {
                    mEditText.setText(num.substring(0, num.length() - 1));
                    mEditText.setSelection(mEditText.getText().length());
                }
                break;
        }
    }


    public interface onCallClickListener {
        void onCall(String number);
    }

}
